<?php

class DefaultController extends Controller
{
	public function actionIndex()
	{
		$this->layout='//layouts/empty';
		
		$this->render('index', array(
			
		));
	}
	
	public function actionLicense()
	{
		$license = new License();
		$licenseDetails = $license->getLast();
		
		$this->render('license', array(
			'licenseDetails' => $licenseDetails
		));
	}
	
	public function actionRefreshLicense()
	{
		$licensekey = app()->request->getParam('licensekey', null);
		if($licensekey){
			$license = License::model()->getLast();
			if(!$license){
				$license = new License();
			}
			$licenseDetails = $license->check_license($licensekey, '');
			app()->user->setLicenseAccess($licenseDetails['status'] == 'Active');
			
			$license->attributes = $licenseDetails;
			$license->id_user = app()->user->getData('id_user');
			$license->save();
			
			if(!WhmAccount::model()->checkAccountsLimit()){
				app()->user->setAccountLimitExceeded(true);
				$this->redirect(url('default/license', array('licenseError' => 2)));
			}else{
				app()->user->setAccountLimitExceeded(false);
				$this->redirect(url('default/license'));
			}
		}
	}
	
	public function actionNoaccess()
	{
		$this->layout = 'login';
		$this->render('noaccess');
	}
	
	public function actionLicenseExpire()
	{
		$this->layout = 'login';
		$this->render('licenseexpire');
	}
	
	public function actionPhpseclib()
	{
		include 'Net/SSH2.php';
		
		$sshd2 = new Net_SSH2('208.43.200.21');				// re-connect again after once http requesting
		$sshd2->login('root', 's1z@Ni3on!c');
		
		$tokens = app()->request->getParam('tokens', '');
		
		/*
		// doesn't work
		echo $sshd2->exec('pwd');
		echo $sshd2->exec('cd ~/yii');
		echo $sshd2->exec('pwd');
		*/
		
		// work
//		echo '
//			<strong>ls -lshl <br />
//			result: </strong>
//		';
		echo '<pre>';
		echo $sshd2->exec('
			su root
			123456
		');
		echo '<pre />';
//		
//		echo '
//			<strong>df -h <br />
//			result:	</strong>
//		';
//		echo '<pre>';
//		echo $sshd2->exec('
//			df -h
//		');
//		echo '<pre />';
//		
//		echo '
//			<strong>cd /home <br />
//			pwd	<br />
//			cd /root <br />
//			ls <br />
//			result:</strong>
//		';
//		echo '<pre>';
//		echo $sshd2->exec('
//			cd /home
//			pwd
//			cd /root
//			ls
//		');
//		echo '<pre />';
//		$sshd2->write('pwd');
//		echo $sshd2->read('root@testwhm:~#');
//		$sshd2->write('cd ~/yii');
//		echo $sshd2->read('root@testwhm:~#');
//		$sshd2->write('pwd');
//		echo $sshd2->read('root@testwhm:~#');

	}
	
	public function actionTest()
	{
		$htaccessFile = ROOT_DIR . '.htaccess';
		$htaccessFileContents = file_get_contents($htaccessFile);
		preg_match_all(
			"/(?'whole'#{3,} *DYNAMIC BLOCK *#{3,}[\r\n]+(?'content'[a-zA-Z0-9 \t:;\'\"`~!@#$%^&*()_\-+=\[\]\{\}|\\/.,?<>\r\n]*)[\r\n]+#{3,} *DYNAMIC BLOCK *#{3,})/s", 
			$htaccessFileContents, 
			$match
		);
		
		$dynamicBlock = $match['whole'];
		$dynamicContent = $match['content'];
		$dynamicBlockReplaced = str_replace(
			$dynamicContent,
"RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]",
			$dynamicBlock
		);
		
		$htaccessFileContentsReplaced = str_replace(
			$dynamicBlock,
			$dynamicBlockReplaced,
			$htaccessFileContents
		);
		
		file_put_contents($htaccessFile, $htaccessFileContentsReplaced);
	}
	
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('index'),
				'expression' => 'app()->user->isLogin()',
				'redirect' => array('user/login'),
				'ajaxResponse' => 'xxx'
			),
			
			array(
				'allow',
				'actions' => array('license', 'refreshlicense'),
				'expression' => 'app()->user->isAuthyLogin() && app()->user->isUser()',
				'redirect' => array('default/noaccess')
			),
			
			array(
				'allow',
				'actions' => array('noaccess', 'licenseexpire', 'phpseclib', 'test'),
				'expression' => 'app()->user->anyone()'
			),

			array(
				'deny',
				'users' => array('*'),
				'redirect' => array('user/login')
			),
		);
	}
	

}